package com.dong.service;

import com.dong.framework.BeanPostProcessor;
import com.dong.framework.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class LubanBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(final Object bean, String beanName) {
        Object re=null;
        if(beanName.equals("orderService")){
             re= Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("Hello");
                    Object result = method.invoke(bean, args);
                    System.out.println("END");
                    return result;
                }
            });
        }
       if(re!=null) {
           return re;
       }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return null;
    }
}
